package com.gmq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmq.common.R;
import com.gmq.entity.Employee;
import com.gmq.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //将密码进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //查询用户名
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //设置条件(只需要用户名)
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(queryWrapper);
        //没有查询到返回失败结果
        if (one == null) {
            return R.error("用户不存在");
        }
        //密码对比
        if (!one.getPassword().equals(password)) {
            return R.error("密码错误");
        }
        //查看员工状态,查看是否停用
        if (one.getStatus() == 0) {
            return R.error("账户停用");
        }

        //登录成功,存入缓存,记录当前登录的用户
        request.getSession().setAttribute("employee", one.getId());
        //返回登录结果
        log.info("{}用户登录", employee.getUsername());
        return R.success(one);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //清理session中当前保存的登录员工对象
        String id = request.getSession().getId();
        log.info("{}退出", id);
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpServletRequest request) {
        log.info("新增员工{}", employee);
        //设置一个初始密码,需要加密
        String password = "123456";
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(password);
        employeeService.save(employee);
        return R.success("添加员工成功");
    }

    @GetMapping("/page")
    public R<Page> pageR(int page,int pageSize,String name){
        log.info("查询员工表{},{}",page,pageSize);
        //分页构造器
        Page page1=new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper();
        //添加过滤条件
        //name不等于空
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //传入分页器,查询条件
        employeeService.page(page1,queryWrapper);
        return R.success(page1);
    }

    @PutMapping
    public R<String> updateStatus(@RequestBody Employee employee,HttpServletRequest request){
        log.info("修改员工{}",employee);
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> employeeR(@PathVariable String id){
        log.info("查询员工明细{}",id);
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }

}
